import turtle as t
import random
import easygui as e
#
t.setup(1136,640)
t.bgpic('bg.png')
#
t.pencolor('white')
t.pensize(5)
t.pu()
t.goto(-200,150)
t.pd()
t.goto(-200,200)
t.goto(200,200)
t.goto(200,150)
for i in range(-100,101,100):
    t.pu()
    t.goto(i,200)
    t.pd()
    t.goto(i,150)
t.pu()
t.goto(-200,175)
t.pd()
t.goto(200, 175)
t.hideturtle()
#
ball = t.Pen()
ball.shape('circle')
ball.color('white')
ball.shapesize(2)
ball.penup()
#
p1 = t.Pen()
p1.shape('turtle')
p1.color('red')
p1.shapesize(2)
p1.setheading(90)
p1.penup()
p1.goto(-200, -280)
#
p2 = t.Pen()
p2.shape('turtle')
p2.color('blue')
p2.shapesize(2)
p2.setheading(90)
p2.penup()
p2.goto(200, -280)
#
def up1():
    p1.setheading(90)
    p1.forward(50)
    if abs(p1.xcor()-ball.xcor())<50 and abs(p1.ycor()-ball.ycor())<50:
        ball.setheading(90)
        ball.forward(random.randint(50,200))
    if ball.ycor()>150 and ball.xcor()>-200 and ball.xcor()<200:
        e.msgbox('玩家一进球','得分')
    if ball.xcor()<-568 or ball.xcor()>568 or ball.ycor()<-320 or ball.ycor()>150:
        ball.goto(random.randint(-400,400),random.randint(-300,150))
def left1():
    p1.setheading(180)
    p1.forward(50)
    if abs(p1.xcor()-ball.xcor()) < 50 and abs(p1.ycor()-ball.ycor()) < 50:
        ball.setheading(180)
        ball.forward(random.randint(50, 200))
        if ball.xcor() < -568 or ball.xcor() > 568 or ball.ycor() < -320 or ball.ycor() > 150:
            ball.goto(random.randint(-400,400),random.randint(-300,150))
def right1():
    p1.setheading(0)
    p1.forward(50)
    if abs(p1.xcor()-ball.xcor()) < 50 and abs(p1.ycor()-ball.ycor()) < 50:
        ball.setheading(0)
        ball.forward(random.randint(50, 200))
        if ball.xcor() < -568 or ball.xcor() > 568 or ball.ycor() < -320 or ball.ycor() > 150:
            ball.goto(random.randint(-400,400),random.randint(-300,150))
def down1():
    p1.setheading(-90)
    p1.forward(50)
    if abs(p1.xcor()-ball.xcor()) < 50 and abs(p1.ycor()-ball.ycor()) < 50:
        ball.setheading(-90)
        ball.forward(random.randint(50, 200))
        if ball.xcor() < -568 or ball.xcor() > 568 or ball.ycor() < -320 or ball.ycor() > 150:
            ball.goto(random.randint(-400,400),random.randint(-300,150))
#
t.onkey(up1, "w")
t.onkey(left1, "a")
t.onkey(right1, "d")
t.onkey(down1, "s")


def up2():
    p2.setheading(90)
    p2.forward(50)
    if abs(p2.xcor()-ball.xcor()) < 50 and abs(p2.ycor()-ball.ycor()) < 50:
        ball.setheading(90)
        ball.forward(random.randint(50, 200))
    if ball.ycor() > 150 and ball.xcor() > -200 and ball.xcor() < 200:
        e.msgbox('玩家二进球', '得分')
    if ball.xcor() < -568 or ball.xcor() > 568 or ball.ycor() < -320 or ball.ycor() > 150:
        ball.goto(random.randint(-400,400),random.randint(-300,150))


def left2():
    p2.setheading(180)
    p2.forward(50)
    if abs(p2.xcor()-ball.xcor()) < 50 and abs(p2.ycor()-ball.ycor()) < 50:
        ball.setheading(180)
        ball.forward(random.randint(50, 200))
        if ball.xcor() < -568 or ball.xcor() > 568 or ball.ycor() < -320 or ball.ycor() > 150:
            ball.goto(random.randint(-400,400),random.randint(-300,150))


def right2():
    p2.setheading(0)
    p2.forward(50)
    if abs(p2.xcor()-ball.xcor()) < 50 and abs(p2.ycor()-ball.ycor()) < 50:
        ball.setheading(0)
        ball.forward(random.randint(50, 200))
        if ball.xcor() < -568 or ball.xcor() > 568 or ball.ycor() < -320 or ball.ycor() > 150:
            ball.goto(random.randint(-400,400),random.randint(-300,150))


def down2():
    p2.setheading(-90)
    p2.forward(50)
    if abs(p2.xcor()-ball.xcor()) < 50 and abs(p2.ycor()-ball.ycor()) < 50:
        ball.setheading(-90)
        ball.forward(random.randint(50, 200))
        if ball.xcor() < -568 or ball.xcor() > 568 or ball.ycor() < -320 or ball.ycor() > 150:
            ball.goto(random.randint(-400,400),random.randint(-300,150))


t.onkey(up2, "Up")
t.onkey(left2, "Left")
t.onkey(right2, "Right")
t.onkey(down2, "Down")

t.listen()
t.done()
